package com.yunxin.zm.controller;

import com.yunxin.common.utils.ResponseResult;
import com.yunxin.zm.enums.GitBranchType;
import com.yunxin.zm.util.GitUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class CommitController {

    @GetMapping("/commit")
    public ResponseResult<Void> commit() throws GitAPIException, IOException {
        String gitUrl = "https://gitee.com/weidabianchen/zm-xiao-shuo.git";
        String username = "1730959485@qq.com";
        String password = "hjl1730959485";
        String localDir = "/www/jenkins/jenkins_home/workspace/zm";
        CredentialsProvider credential = GitUtils.createCredentialsProvider(username, password);
        Git git = GitUtils.getGit(gitUrl, credential, localDir);
        git.checkout().setCreateBranch(false).setName(GitBranchType.LOCAL.name()).call();
        boolean commitFlag = GitUtils.commitFiles(git, credential, ".", "test");
        GitUtils.closeGit(git);
        return ResponseResult.success();
    }
}
